package com.woniuxy.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.business.config.MessageProducer;
import com.woniuxy.business.dao.ActivityDao;
import com.woniuxy.business.dao.MessageDao;
import com.woniuxy.business.service.ActivityService;
import com.woniuxy.business.service.MessageService;
import com.woniuxy.commons.entity.Message;
import com.woniuxy.commons.entity.QueryEntity;
import com.woniuxy.commons.utils.ResStatus;
import com.woniuxy.commons.utils.ResponseResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 雷珉
 * @date 2022年06月08日 16:30
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    private MessageDao messageDao;
    @Resource
    private MessageProducer messageProducer;

    @Override
    public ResponseResult<List<Message>> findAll() {
        List<Message> all = messageDao.findAll();
        return new ResponseResult<>(200,"查询成功",all,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult<PageInfo<Message>> findAllByCondition(QueryEntity<Message> queryEntity) {
        //开启分页
        PageHelper.startPage(queryEntity.getCurrentPage(),queryEntity.getPageSize());
        //排序
        String orderSort = queryEntity.getOrderSort();
        if("asc".equalsIgnoreCase(orderSort) || "desc".equalsIgnoreCase(orderSort)){
            PageHelper.orderBy(queryEntity.getOrderKey()+" "+orderSort);
        }
        //封装数据
        List<Message> all = messageDao.findAllByCondition(queryEntity.getEntity());
        PageInfo<Message> pageInfo = PageInfo.of(all);
        return new ResponseResult<>(200,"查询成功",pageInfo,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult<Message> findOne(int id) {
        Message one = messageDao.findOne(id);
        return new ResponseResult<>(200,"查询成功",one,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult addOne(Message message) {
        messageDao.addOne(message);
        return new ResponseResult<>(200,"添加成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult deleteOne(int id) {
        messageDao.deleteOne(id);
        return new ResponseResult<>(200,"删除成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult deleteBatch(int[] ids) {
        messageDao.deleteBatch(ids);
        return new ResponseResult<>(200,"删除成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult updateOne(Message message) {
        messageDao.updateOne(message);
        return new ResponseResult<>(200,"更新成功",null,ResStatus.SUCCESS);
    }

    @Override
    public ResponseResult send(int messageId, int[] userIds) {
        //根据消息类型推送进不同的队列
        Message message = messageDao.findOne(messageId);
        String type = message.getType();
        if("短信".equals(type)){
            for (int id:userIds) {
                messageProducer.send(messageId+":"+id,"sms");
            }
        }else if("邮件".equals(type)){
            for (int id:userIds) {
                messageProducer.send(messageId+":"+id,"email");
            }
        }else if("站内信".equals(type)){
            for (int id:userIds) {
                messageProducer.send(messageId+":"+id,"news");
            }
        }

        return new ResponseResult<>(200,"发送成功",null,ResStatus.SUCCESS);
    }
}

